package vmr_client

import (
	"fmt"
	"git.supremind.info/product/box/vehicle/global"
	"git.supremind.info/product/box/vehicle/internal/dto"
	"git.supremind.info/product/box/vehicle/internal/util"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
)

//保护性存储大小接口
func SystemsStorage(c *gin.Context) {
	var req = dto.SystemStorage{}
	if c.BindJSON(&req) != nil {
		c.JSON(http.StatusOK, dto.Result{Code: -1, Message: "传参有误"})
		return
	}
	url := fmt.Sprintf("%s/v1/system/storage", global.VmrSetting.Host)
	res, err := util.PostData(url, req)
	if err != nil {
		log.Println(err)
		c.JSON(http.StatusOK, dto.Result{Code: -1, Message: err.Error()})
	} else {
		c.JSON(http.StatusOK, res)
	}
}

func GetTimeNtp(c *gin.Context) {
	url := fmt.Sprintf("%s/v1/time-sync/ntp", global.VmrSetting.Host)
	res, err := util.GetData(url)
	if err != nil {
		c.JSON(http.StatusOK, dto.Result{Code: -1, Message: err.Error()})
	} else {
		c.JSON(http.StatusOK, res)
	}
}

func GetTimeZone(c *gin.Context) {
	url := fmt.Sprintf("%s/v1/time-sync/timezone", global.VmrSetting.Host)
	res, err := util.GetData(url)
	if err != nil {
		c.JSON(http.StatusOK, dto.Result{Code: -1, Message: err.Error()})
	} else {
		c.JSON(http.StatusOK, res)
	}
}

func GetTimeZoneList(c *gin.Context) {
	url := fmt.Sprintf("%s/v1/time-sync/timezone/list", global.VmrSetting.Host)
	res, err := util.GetData(url)
	if err != nil {
		c.JSON(http.StatusOK, dto.Result{Code: -1, Message: err.Error()})
	} else {
		c.JSON(http.StatusOK, res)
	}
}

//查询盒子信息
func QueryBoxInfo(c *gin.Context) {
	url := fmt.Sprintf("%s/v1/box", global.VmrSetting.Host)
	res, err := util.GetData(url)
	if err != nil {
		log.Println(err)
		c.JSON(http.StatusOK, dto.Result{Code: -1, Message: err.Error()})
	} else {
		c.JSON(http.StatusOK, res)
	}
}

//修改盒子信息
func UpdateBox(c *gin.Context) {
	var req = dto.BoxMetaData{}
	if c.BindJSON(&req) != nil {
		c.JSON(http.StatusOK, dto.Result{Code: -1, Message: "传参有误"})
		return
	}
	url := fmt.Sprintf("%s/v1/box/meta", global.VmrSetting.Host)
	res, err := util.PostData(url, req)
	if err != nil {
		c.JSON(http.StatusOK, dto.Result{Code: -1, Message: err.Error()})
	} else {
		c.JSON(http.StatusOK, res)
	}
}

//设置冗余信息
func SetExtraData(c *gin.Context) {
	var req = dto.Extra{}
	if c.BindJSON(&req) != nil {
		c.JSON(http.StatusOK, dto.Result{Code: -1, Message: "传参有误"})
		return
	}
	url := fmt.Sprintf("%s/v1/model/extra", global.VmrSetting.Host)
	res, err := util.PutData(url, req)
	if err != nil {
		c.JSON(http.StatusOK, dto.Result{Code: -1, Message: err.Error()})
	} else {
		c.JSON(http.StatusOK, res)
	}
}

//获取冗余信息
func GetExtraData(c *gin.Context) {
	url := fmt.Sprintf("%s/v1/model/extra", global.VmrSetting.Host)
	res, err := util.GetData(url)
	if err != nil {
		c.JSON(http.StatusOK, dto.Result{Code: -1, Message: err.Error()})
	} else {
		c.JSON(http.StatusOK, res)
	}
}

//设置车身任务
func SetTask(c *gin.Context) {
	var req = dto.Task{}
	if c.BindJSON(&req) != nil {
		c.JSON(http.StatusOK, dto.Result{Code: -1, Message: "传参有误"})
		return
	}
	url := fmt.Sprintf("%s/v1/model/task", global.VmrSetting.Host)
	res, err := util.PutData(url, req)
	if err != nil {
		c.JSON(http.StatusOK, dto.Result{Code: -1, Message: err.Error()})
	} else {
		c.JSON(http.StatusOK, res)
	}
}

//获取车身任务
func GetTask(c *gin.Context) {
	url := fmt.Sprintf("%s/v1/model/service/status", global.VmrSetting.Host)
	res, err := util.GetData(url)
	if err != nil {
		c.JSON(http.StatusOK, dto.Result{Code: -1, Message: err.Error()})
	} else {
		c.JSON(http.StatusOK, res)
	}
}

//获取车身摄像头配置
func GetSideCameraConfig(c *gin.Context) {
	url := fmt.Sprintf("%s/v1/model/side_camera/config", global.VmrSetting.Host)
	res, err := util.GetData(url)
	if err != nil {
		c.JSON(http.StatusOK, dto.Result{Code: -1, Message: err.Error()})
	} else {
		c.JSON(http.StatusOK, res)
	}
}

//设置车身摄像头配置
func SideCameraConfig(c *gin.Context) {
	var req = dto.CommonParam{}
	if c.BindJSON(&req) != nil {
		c.JSON(http.StatusOK, dto.Result{Code: -1, Message: "传参有误"})
		return
	}
	url := fmt.Sprintf("%s/v1/model/side_camera/config", global.VmrSetting.Host)
	res, err := util.PutData(url, req)
	if err != nil {
		c.JSON(http.StatusOK, dto.Result{Code: -1, Message: err.Error()})
	} else {
		c.JSON(http.StatusOK, res)
	}
}

//获取车身相机抓拍图
func GetSideCameraCapture(c *gin.Context) {
	var req = dto.CommonParam{}
	if c.BindJSON(&req) != nil {
		c.JSON(http.StatusOK, dto.Result{Code: -1, Message: "传参有误"})
		return
	}
	url := fmt.Sprintf("%s/v1/model/side_camera/capture", global.VmrSetting.Host)
	res, err := util.PostData(url, req)
	if err != nil {
		c.JSON(http.StatusOK, dto.Result{Code: -1, Message: err.Error()})
	} else {
		c.JSON(http.StatusOK, res)
	}
}

//获取车身摄像头曝光值
func GetSideCameraExposure(c *gin.Context) {
	url := fmt.Sprintf("%s/v1/model/side_camera/exposure", global.VmrSetting.Host)
	res, err := util.GetData(url)
	if err != nil {
		c.JSON(http.StatusOK, dto.Result{Code: -1, Message: err.Error()})
	} else {
		c.JSON(http.StatusOK, res)
	}
}

//获取设备状态
func GetDeviceStatus(c *gin.Context) {
	url := fmt.Sprintf("%s/v1/model/service/status", global.VmrSetting.Host)
	res, err := util.GetData(url)
	if err != nil {
		c.JSON(http.StatusOK, dto.Result{Code: -1, Message: err.Error()})
	} else {
		c.JSON(http.StatusOK, res)
	}
}

//开启服务（进入运行状态）
func StartService(c *gin.Context) {
	url := fmt.Sprintf("%s/v1/model/service/start", global.VmrSetting.Host)
	res, err := util.PostWithOutBody(url)
	if err != nil {
		c.JSON(http.StatusOK, dto.Result{Code: -1, Message: err.Error()})
	} else {
		c.JSON(http.StatusOK, res)
	}
}

//停止服务（进入配置状态）
func StopService(c *gin.Context) {
	url := fmt.Sprintf("%s/v1/model/service/stop", global.VmrSetting.Host)
	res, err := util.PostWithOutBody(url)
	if err != nil {
		c.JSON(http.StatusOK, dto.Result{Code: -1, Message: err.Error()})
	} else {
		c.JSON(http.StatusOK, res)
	}
}

//查询默认接口的ipv4地址和dns服务器
func QueryIpv4(c *gin.Context) {
	//网口类型，取值为eth0/eth1
	eth := c.Query("network_type")
	url := fmt.Sprintf("%s/v1/model/ipv4?network_type=%s", global.VmrSetting.Host, eth)
	res, err := util.GetData(url)
	if err != nil {
		c.JSON(http.StatusOK, dto.Result{Code: -1, Message: err.Error()})
	} else {
		c.JSON(http.StatusOK, res)
	}
}

//配置默认接口的ipv4地址和dns服务器, 执行成功会自动关闭dhcp
func UpdateIpv4(c *gin.Context) {
	var req = dto.IPV4Address{}
	if c.BindJSON(&req) != nil {
		c.JSON(http.StatusOK, dto.Result{Code: -1, Message: "传参有误"})
		return
	}
	url := fmt.Sprintf("%s/v1/network/ipv4", global.VmrSetting.Host)
	res, err := util.PostData(url, req)
	if err != nil {
		c.JSON(http.StatusOK, dto.Result{Code: -1, Message: err.Error()})
	} else {
		c.JSON(http.StatusOK, res)
	}
}

//设置摄像头
func SetCamera(c *gin.Context) {
	locationType := c.Param("location_type")
	if len(locationType) == 0 {
		c.JSON(http.StatusOK, dto.Result{Code: -1, Message: "缺少 location_type 参数"})
		return
	}
	var params dto.Camera
	if err := c.Bind(&params); err != nil {
		c.JSON(http.StatusOK, dto.Result{Code: -1, Message: "传参有误"})
		return
	}
	url := fmt.Sprintf("%s/v1/model/camera/%s", global.VmrSetting.Host, locationType)
	res, err := util.PostData(url, params)
	if err != nil {
		c.JSON(http.StatusOK, dto.Result{Code: -1, Message: err.Error()})
	} else {
		c.JSON(http.StatusOK, res)
	}
}

//获取摄像头
func GetCamera(c *gin.Context) {
	url := fmt.Sprintf("%s/v1/model/camera", global.VmrSetting.Host)
	res, err := util.GetData(url)
	if err != nil {
		c.JSON(http.StatusOK, dto.Result{Code: -1, Message: err.Error()})
	} else {
		c.JSON(http.StatusOK, res)
	}
}

//获取摄像头状态
func GetCameraStatus(c *gin.Context) {
	url := fmt.Sprintf("%s/v1/model/camera/status", global.VmrSetting.Host)
	res, err := util.GetData(url)
	if err != nil {
		c.JSON(http.StatusOK, dto.Result{Code: -1, Message: err.Error()})
	} else {
		c.JSON(http.StatusOK, res)
	}
}
